package com.ixuestar.controller;

import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.ixuestar.service.IxuestarEasyLearnService;
import com.ixuestar.util.ReturnResult;
import com.ixuestar.wxutil.GetwxOpenId;
import com.ixuestar.wxutil.PayConfigUtil;

@CrossOrigin
@RestController
@RequestMapping(value = "/easyLearn",method = RequestMethod.POST)
public class IxuestarEasyLearnController extends BaseController{

	@Autowired
	private IxuestarEasyLearnService ixuestarEasyLearnService;
	
	public Logger logger = Logger.getLogger(IxuestarEasyLearnController.class);
	
	/**
	 * @method 通过手机号获取老师信息
	 * @param jsonObject
	 * @param response
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/getWxSessionKey")
	public Map<String, Object> getWxSessionKey(@RequestBody JSONObject jsonObject,HttpServletResponse response){
		//创建返回参数
		Map<String, Object> map = null;
		try {
			String appId = PayConfigUtil.ixuestarwx_APP_ID_Y;
			String appSec = PayConfigUtil.ixuestarwx_APP_SEC_Y;
			logger.info("-----------进入getWxSessionKey接口-------------");
			String code = jsonObject.getString("code");
			//执行接口
			JSONObject wxSessionKey = GetwxOpenId.getWxSessionKey(code,appId,appSec);
			//
			map = ReturnResult.correctResult(wxSessionKey);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			map = ReturnResult.wrongResult();
		}
		return map;
	}
	
	/**
	 * @method 通过手机号信息
	 * @param jsonObject
	 * @param response
	 * @return
	 */
	@ResponseBody
	@ExceptionHandler
	@RequestMapping("/getWxuserInfo")
	public Map<String, Object> getWxTelephone(@RequestBody JSONObject jsonObject,HttpServletResponse response){
		//创建返回参数
		Map<String, Object> map = null;
		//非空判断
		this.validateObjectNullException(jsonObject, "参数不能为空");
		logger.info("-----------进入getWxTelephone接口-------------");
		//请求接口
		JSONObject wxuserInfo = ixuestarEasyLearnService.getWxuserInfo(jsonObject);
		Integer code = wxuserInfo.getInteger("code");
		JSONObject yxWxuser = wxuserInfo.getJSONObject("yxWxuser");
		if (code==0) {
			yxWxuser.remove("code");
			map = ReturnResult.otherJSONObjectResult(1, yxWxuser, "用户的第一次登陆");
		}else if (code==1) {
			map = ReturnResult.otherResult(3, "未获取到用户的手机对象");
		}else if (code==2) {
			yxWxuser.remove("code");
			map = ReturnResult.otherJSONObjectResult(2, yxWxuser, "用户未绑定学生信息");
		}else if (code==3) {
			yxWxuser.remove("code");
			map = ReturnResult.correctResult(yxWxuser);
		}
		return map;
	}
	
	/**
	 * @method 获取学校列表
	 * @param jsonObject
	 * @param response
	 * @return
	 */
	@ResponseBody
	@ExceptionHandler
	@RequestMapping(value = "/getSchool")
	public Map<String, Object> getSchool(@RequestBody JSONObject jsonObject,HttpServletResponse response){
		Map<String, Object> map = null;
		//非空判断
		this.validateObjectNullException(jsonObject, "对象中参数不能为空");
		//请求接口
		JSONObject schoolList = ixuestarEasyLearnService.getSchoolInfos();
		//返回正确结果
		map = ReturnResult.correctResult(schoolList);
		return map;
	}
	
	/**
	 * @method获取学校年级
	 * @param jsonObject
	 * @param response
	 * @return
	 */
	@ResponseBody
	@ExceptionHandler
	@RequestMapping("/getSchoolGrade")
	public Map<String, Object> getSchoolGrade(@RequestBody JSONObject jsonObject,HttpServletResponse response){
		Map<String, Object> map = null;
		//非空判断
		this.validateObjectNullException(jsonObject, "对象中参数不能为空");
		//请求接口
		JSONObject schoolGradeList = ixuestarEasyLearnService.getSchoolGradeList(jsonObject);
		//正确返回
		map = ReturnResult.correctResult(schoolGradeList);
		return map;
	}
	
	/**
	 * @method获取年级下的班级
	 * @param jsonObject
	 * @param response
	 * @return
	 */
	@ResponseBody
	@ExceptionHandler
	@RequestMapping("/getSchoolClass")
	public Map<String, Object> getSchoolClass(@RequestBody JSONObject jsonObject,HttpServletResponse response){
		Map<String, Object> map = null;
		//非空判断
		this.validateObjectNullException(jsonObject, "对象中参数不能为空");
		//请求班级列表
		JSONObject schoolClassList = ixuestarEasyLearnService.getSchoolClassList(jsonObject);
		//正确返回
		map = ReturnResult.correctResult(schoolClassList);
		return map;
	}
	
	/**
	 * @method 获取班级的学生信息
	 * @param jsonObject
	 * @param response
	 * @return
	 */
	@ResponseBody
	@ExceptionHandler
	@RequestMapping("/getSchoolStudent")
	public Map<String, Object> getSchoolStudent(@RequestBody JSONObject jsonObject,HttpServletResponse response){
		Map<String, Object> map = null;
		//非空判断
		this.validateObjectNullException(jsonObject, "对象中参数不能为空");
		//接口请求							
		JSONObject schoolStudentList = ixuestarEasyLearnService.getSchoolStudentList(jsonObject);
		//正确返回
		map = ReturnResult.correctResult(schoolStudentList);

		return map;
	} 
	
	/**
	 * @method 绑定信息
	 * @param jsonObject
	 * @param response
	 * @return
	 */
	@ResponseBody
	@ExceptionHandler
	@RequestMapping("/bindingWxuserInfo")
	public Map<String, Object> bindingWxuserInfo(@RequestBody JSONObject jsonObject,HttpServletResponse response){
		Map<String, Object> map = null;
		//非空判断
		this.validateObjectNullException(jsonObject, "对象中参数不能为空");
		//接口请求							
		JSONObject bindingWxuserInfo = ixuestarEasyLearnService.bindingWxuserInfo(jsonObject);
		//获取code
		Integer code = bindingWxuserInfo.getInteger("code");
		if (code==0) {
			JSONObject yxWxuser = bindingWxuserInfo.getJSONObject("yxWxuser");
			map = ReturnResult.correctResult(yxWxuser);
		}else {
			map = ReturnResult.otherResult(code, "ywId不正确,请核实后重新使用");
		}
		return map;
	} 
	
	/**
	 * @method 视频列表
	 * @param jsonObject
	 * @param response
	 * @return
	 */
	@ResponseBody
	@ExceptionHandler
	@RequestMapping("/getVideos")
	public Map<String, Object> getVideos(@RequestBody JSONObject jsonObject,HttpServletResponse response){
		Map<String, Object> map = null;
		//非空判断
		this.validateObjectNullException(jsonObject, "对象中参数不能为空");
		//接口请求							
		JSONObject playVideosList = ixuestarEasyLearnService.getPlayVideo(jsonObject);
		map = ReturnResult.correctResult(playVideosList);
		return map;
	} 
	
	/**
	 * @method 视频列表
	 * @param jsonObject
	 * @param response
	 * @return
	 */
	@ResponseBody
	@ExceptionHandler
	@RequestMapping("/getKnowledgevideos")
	public Map<String, Object> getKnowledgevideos(@RequestBody JSONObject jsonObject,HttpServletResponse response){
		Map<String, Object> map = null;
		//非空判断
		this.validateObjectNullException(jsonObject, "对象中参数不能为空");
		//接口请求							
		JSONObject selectedList = ixuestarEasyLearnService.getSelectedList(jsonObject, 0);
		Integer code = selectedList.getInteger("code");
		if (code==0) {
			selectedList.remove("code");
			//返回列表
			map = ReturnResult.correctResult(selectedList);			
		}else if (code==1) {
			map = ReturnResult.otherResult(code, "为获取到该用户的学生绑定信息");
		}else if (code==2) {
			map = ReturnResult.otherResult(code, "为获取到该用户的学生信息");
		}else if (code==3) {
			map = ReturnResult.otherResult(code, "为获取到该用户的学生班级信息");
		}
		return map;
	} 
	
	/**
	 * @method 精选视频列表
	 * @param jsonObject
	 * @param response
	 * @return
	 */
	@ResponseBody
	@ExceptionHandler
	@RequestMapping("/getKnowledgeSelectvideos")
	public Map<String, Object> getKnowledgeSelectvideos(@RequestBody JSONObject jsonObject,HttpServletResponse response){
		Map<String, Object> map = null;
		//非空判断
		this.validateObjectNullException(jsonObject, "对象中参数不能为空");
		//接口请求							
		JSONObject selectedList = ixuestarEasyLearnService.getSelectedList(jsonObject, 1);
		Integer code = selectedList.getInteger("code");
		if (code==0) {
			selectedList.remove("code");
			//返回列表
			map = ReturnResult.correctResult(selectedList);			
		}else if (code==1) {
			map = ReturnResult.otherResult(code, "为获取到该用户的学生绑定信息");
		}else if (code==2) {
			map = ReturnResult.otherResult(code, "为获取到该用户的学生信息");
		}else if (code==3) {
			map = ReturnResult.otherResult(code, "为获取到该用户的学生班级信息");
		}
		return map;
	} 
	
	/**
	 * @method 获取专辑视频列表
	 * @param jsonObject
	 * @param response
	 * @return
	 */
	@ResponseBody
	@ExceptionHandler
	@RequestMapping("/getVideoList")
	public Map<String, Object> getVideoList(@RequestBody JSONObject jsonObject,HttpServletResponse response){
		Map<String, Object> map = null;
		//非空判断
		this.validateObjectNullException(jsonObject, "对象中参数不能为空");
		//接口请求							
		JSONObject videoList = ixuestarEasyLearnService.getVideoList(jsonObject);
		map= ReturnResult.correctResult(videoList);
		return map;
	} 
	
	/**
	 * @method 更新播放量和观看历史
	 * @author SLunshine
	 * @param jsonObject
	 * @param response
	 * @return
	 */
	@ResponseBody
	@ExceptionHandler
	@RequestMapping(value = "/writePlaysNum")
	public Map<String, Object> writePlaysNum(@RequestBody JSONObject jsonObject,HttpServletResponse response){
		//返回map
		Map<String, Object> map = null;
		//非空判断
		this.validateObjectNullException(jsonObject, "参数不能为空");
		//调用接口
		JSONObject writePlayhistory = ixuestarEasyLearnService.writePlayhistory(jsonObject);
		//正确返回
		map = ReturnResult.correctResult(writePlayhistory);
		return map;
	}
	
	/**
	 * @method 更新播放时间
	 * @author SLunshine
	 * @param jsonObject
	 * @param response
	 * @return
	 */
	@ResponseBody
	@ExceptionHandler
	@RequestMapping(value = "/writePlayRate")
	public Map<String, Object> writePlayRate(@RequestBody JSONObject jsonObject,HttpServletResponse response){
		//返回map
		Map<String, Object> map = null;
		//非空判断
		this.validateObjectNullException(jsonObject, "参数不能为空");
		//调用接口
		JSONObject writePlayRate = ixuestarEasyLearnService.writePlayRate(jsonObject);
		//正确返回
		map = ReturnResult.correctResult(writePlayRate);
		return map;
	}
	
	/**
	 * @method 写入点赞数据
	 * @author SLunshine
	 * @param jsonObject
	 * @param response
	 * @return
	 */
	@ResponseBody
	@ExceptionHandler
	@RequestMapping(value = "/writeLikesNum")
	public Map<String, Object> writeLikesNum(@RequestBody JSONObject jsonObject,HttpServletResponse response){
		//返回map
		Map<String, Object> map = null;
		//非空判断
		this.validateObjectNullException(jsonObject, "参数不能为空");
		//调用接口
		ixuestarEasyLearnService.writeLikehistory(jsonObject);
		//正确返回
		map = ReturnResult.correctResult(new JSONObject());
		return map;
	}
	
	/**
	 * @method 写入收藏数据
	 * @author SLunshine
	 * @param jsonObject
	 * @param response
	 * @return
	 */
	@ResponseBody
	@ExceptionHandler
	@RequestMapping(value = "/writeCollectsNum")
	public Map<String, Object> writeCollectsNum(@RequestBody JSONObject jsonObject,HttpServletResponse response){
		//返回map
		Map<String, Object> map = null;
		//非空判断
		this.validateObjectNullException(jsonObject, "参数不能为空");
		//调用接口
		ixuestarEasyLearnService.writeCollectHistory(jsonObject);
		//正确返回
		map = ReturnResult.correctResult(new JSONObject());
		return map;
	}
	
	/**
	 * @method 获取播放历史列表
	 * @author SLunshine
	 * @param jsonObject
	 * @param response
	 * @return map
	 */
	@ResponseBody
	@ExceptionHandler
	@RequestMapping(value = "/getPlayHistoryList")
	public Map<String, Object> getPlayHistoryList(@RequestBody JSONObject jsonObject,HttpServletResponse response){
		//返回map
		Map<String, Object> map = null;
		//非空判断
		this.validateObjectNullException(jsonObject, "参数不能为空");
		//调用接口
		JSONObject playVideosList = ixuestarEasyLearnService.getPlayVideosList(jsonObject);
		//正确返回
		map = ReturnResult.correctResult(playVideosList);
		return map;
	}
	
	/**
	 * @method 获取收藏历史列表
	 * @author SLunshine
	 * @param jsonObject
	 * @param response
	 * @return map
	 */
	@ResponseBody
	@ExceptionHandler
	@RequestMapping(value = "/getCollectHistoryList")
	public Map<String, Object> getCollectHistoryList(@RequestBody JSONObject jsonObject,HttpServletResponse response){
		//返回map
		Map<String, Object> map = null;
		//非空判断
		this.validateObjectNullException(jsonObject, "参数不能为空");
		//调用接口
		JSONObject collectVideosList = ixuestarEasyLearnService.getCollectVideosList(jsonObject);
		//正确返回
		map = ReturnResult.correctResult(collectVideosList);
		return map;
	}
	
	/**
	 * @method 终端登录接口
	 * @author SLunshine
	 * @param jsonObject
	 * @param response
	 * @return map
	 */
	@ResponseBody
	@ExceptionHandler
	@RequestMapping(value = "/getLoginYueyixueSmall")
	public Map<String, Object> getLoginYueyixueSmall(@RequestBody JSONObject jsonObject,HttpServletResponse response){
		//返回map
		Map<String, Object> map = null;
		//非空判断
		this.validateObjectNullException(jsonObject, "参数不能为空");
		//调用接口
		JSONObject loginYueyixue = ixuestarEasyLearnService.getLoginYueyixue(jsonObject);
		Integer code = loginYueyixue.getInteger("code");
		if (code==0) {
			//正确返回
			map = ReturnResult.correctResult(loginYueyixue);			
		}else if (code==1) {
			map = ReturnResult.otherResult(code, "号码有误，请重新输入");
		}else if (code==2) {
			map = ReturnResult.otherResult(code, "该号码未绑定学生信息");
		}else if (code==3) {
			map = ReturnResult.otherResult(code, "密码有误，请重新输入");
		}
		return map;
	}
	
	/**
	 * @method 修改密码
	 * @author SLunshine
	 * @param jsonObject
	 * @param response
	 * @return map
	 */
	@ResponseBody
	@ExceptionHandler
	@RequestMapping(value = "/updateMemberPassWord")
	public Map<String, Object> updateMemberPassWord(@RequestBody JSONObject jsonObject,HttpServletResponse response){
		//返回map
		Map<String, Object> map = null;
		//非空判断
		this.validateObjectNullException(jsonObject, "参数不能为空");
		//调用接口
		String userName = jsonObject.getString("userName");
		String passWord = jsonObject.getString("passWord");
		if (userName==null || userName.replace("\\s*", "").equals("") || passWord==null || passWord.replace("\\s*", "").equals("")) {
			map = ReturnResult.otherResult(1, "参数有误或者为空，请核实");
		}else {
			ixuestarEasyLearnService.updateLoginPassWord(userName, passWord);
			//返回正确
			map = ReturnResult.correctResult(new JSONObject());
		}
		return map;
	}
}
